<template>
  <h2>姓名：{{ name }}</h2>
  <h2>姓名：{{ age }}</h2>
  <h2>薪资：{{ job.j1.salary }}</h2>
  <h2>座驾：{{ person.car }}</h2>
  <button @click="name+='!'">修改姓名</button>
  <button @click="addAge">修改年龄</button>
  <button @click="addCar">添加一辆车</button>
  <button @click="changeCarPrice">修改车辆价钱</button>
</template>

<script>
import {ref, reactive, toRefs, toRaw, markRaw} from "vue";

export default {
  name: "Demo",
  setup() {
    let sum = ref(0);
    let person = reactive({
      name: '张三',
      age: 18,
      job: {
        j1: {
          salary: 20
        }
      }
    });

    function addAge() {
      const p = toRaw(person);
      p.age++;
      console.log(p);
    }

    function addCar() {
      let car = {name: '奔驰', price: 40};
      person.car = markRaw(car);
    }

    function changeCarPrice() {
      person.car.price++;
      console.log(person.car);
    }

    return {
      person,
      ...toRefs(person),
      addAge,
      addCar,
      changeCarPrice
    };
  }
}
</script>

<style scoped>

</style>